import 'package:aichat_flutter_wallet/module_common/widgets/dialog/easyloading.dart';
import 'package:flutter/material.dart';
import 'package:lib_cloud_storage/lib_cloudstorage.dart';

/**
 * 测试谷歌云盘功能
 */
class TestGoogleCloud {
  static void testDrive(BuildContext context) {
    Future.delayed(Duration.zero, () {
      showModalBottomSheet(
          context: context,
          builder: (context) {
            return TestDriveDialog();
          });
    });
  }
}

class TestDriveDialog extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => TestDriveDialogState();
}

class TestDriveDialogState extends State<TestDriveDialog> {
  @override
  void initState() {
    super.initState();
  }

  late String email; //从谷歌sdk 中获取的邮箱
  late String fileId; //上传后谷歌云盘id
  void testGoogleDrive() {
    LibCloudStorage.uploadToGoogleCloud(
            email, "this is flutter call google drive cloud", '123456', '1111')
        .then((value) {
      this.fileId = value;
      String tip = "google drive upload fileId：$value";
      print("call google drive success $tip ");
      showToast(tip);
    }).catchError((e) {
      print("call google upload fair: $e");
    });
  }

  void testGoogleDriveFileList() {
    LibCloudStorage.queryGoogleDriveList(email).then((value) {
      print("call google google drive list success ");
    }).catchError((e) {
      print("call google google drive list fair: $e");
    });
  }

  void testGoogleDownloadFile() {
    LibCloudStorage.googleCloudDownloadFile(email, fileId).then((value) {
      print("call google download success, file content: $value ");
    }).catchError((e) {
      print("call google download fair: $e");
    });
  }

  void testGoogleLogin() {
    LibCloudStorage.startGoogleLogin().then((value) {
      String tip = "google email:${value.email}";
      this.email = value.email;
      print("call google login success: ,$tip");
      // showToast(tip);
    }).catchError((e) {
      print("call google drive fair: $e");
    });
  }

  void showToast(String content) {
    EasyLoading.showSuccess(content);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            TextButton(
                onPressed: () {
                  testGoogleLogin();
                },
                child: Text("google login")),
            SizedBox(
              height: 20,
            ),
            TextButton(
                onPressed: () {
                  testGoogleDrive();
                },
                child: Text("google drive upload file")),
            SizedBox(
              height: 20,
            ),
            TextButton(
                onPressed: () {
                  testGoogleDownloadFile();
                },
                child: Text("google drive download file")),
            SizedBox(
              height: 20,
            ),
            TextButton(
                onPressed: () {
                  testGoogleDriveFileList();
                },
                child: Text("google query drive file list ")),
          ],
        ),
        padding: EdgeInsets.all(20),
      ),
    );
  }
}
